import os
import re
import shutil


def genMessageConst(fileName):
    fileObj = open(fileName, "r")
    line = fileObj.readline()
    fileObj2 = open("MessageIDConst.h", "w")
    fileObj3 = open("MessageIDConst.lua", "w")
    fileObj4 = open("MessageConst.h", "w")
    fileObj5 = open("MessageConst.lua", "w")

    fileObj2.write("#pragma once\n\n")

    fileObj3.write('''local MessageIDConst= {} \n
    ''')

    fileObj4.write('''#include"MessageConst.h"\n\n
    std::string MessageConst(int id)
    {
    switch (id)
    {''')

    fileObj5.write('''local MessageConst = {
    ''')

    while (line):
        mathContent = re.search("[\s\t]*ID_(\w+)[\s\t]*=[\s\t]*(\d+).*", line)
        if mathContent:
            ids = line.split("=")
            id_str = ids[0].replace("ID_", "").replace(" ", "")
            id_value = ids[1].replace(" ", "").replace(";", "")
            print(id_str)
            print(int(id_value))
            fileObj2.writelines("const static int " + line)
            fileObj3.writelines("MessageIDConst.ID_" + id_str + "=" + id_value)
            fileObj4.write("case ID_" + id_str + ":\n" + "return \"" + id_str + "\";\n")
            fileObj5.write("  [" + str(int(id_value)) + "]=\"" + id_str + "\",\n")

        line = fileObj.readline()

    fileObj3.write("\n\nreturn MessageIDConst")
    fileObj4.write('''	default:
    return "";
    }\n}''')
    fileObj5.write("\n}\nreturn MessageConst")
    fileObj.close()
    fileObj2.close()
    fileObj3.close()
    fileObj4.close()
    fileObj5.close()

    shutil.copy("MessageIDConst.h", "../GameServer/GameServer/MessageIDConst.h")
    shutil.copy("MessageConst.h", "../GameServer/GameServer/MessageConst.cpp")
    shutil.copy("MessageIDConst.lua", "../Assets/LuaScripts/const/MessageIDConst.lua")
    shutil.copy("MessageConst.lua", "../Assets/LuaScripts/const/MessageConst.lua")


if (__name__ == "__main__"):
    genMessageConst("mmorpg.proto")
    os.system("pause")
    pass
